package com.heatingcharge.service.charge.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.heatingcharge.dto.*;
import com.heatingcharge.entity.charge.ChargeInfo;
import com.heatingcharge.entity.charge.ClearOff;
import com.heatingcharge.mapper.charge.ChargeInfoMapper;
import com.heatingcharge.mapper.charge.ChargePayableMapper;
import com.heatingcharge.mapper.charge.ClearOffMapper;
import com.heatingcharge.param.SearchParam;
import com.heatingcharge.service.charge.IChargePayableService;
import com.heatingcharge.service.charge.IClearOffService;
import org.apache.commons.collections.MultiMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author devin.jiang
 * @since 2019-10-17
 */
@Service
public class ClearOffServiceImpl extends ServiceImpl<ClearOffMapper, ClearOff> implements IClearOffService {
    @Autowired
    IChargePayableService chargePayableService;

    @Autowired
    ClearOffMapper clearOffMapper;

    @Override
    public ZoneClearOffDTO getAllZonesStatistics(SearchParam searchParam) {

        return clearOffMapper.selectAllZonesStatistics(searchParam);
    }

    @Override
    public Map<String, Object> getArreasMap(SearchParam searchParam) {
        Map<String, Object> mapData=new HashMap<>();
        List<UserArrearsDTO> userArrearsDTOS= Lists.newArrayList();
        //1 查询欠费信息
        List<ChargePayableDTO> arreasInfo = chargePayableService.getArreasInfo(searchParam);
        Multimap<Integer,ChargePayableDTO> multimap=ArrayListMultimap.create();
        Map<Integer,String> houseIdMap=new HashMap<>();
        if (arreasInfo.size()==0){
            return null;
        }
        arreasInfo.stream().forEach(dto->{
            multimap.put(dto.getHouseId(),dto);
            String name=dto.getZoneName()+dto.getBuildNum()+dto.getUnitNum()+dto.getHouseNum()+dto.getCusName();
            houseIdMap.put(dto.getHouseId(),name);
        });

        Iterator<Integer> iter = houseIdMap.keySet().iterator();
//        UserArrearsDTO userArrearsDTO=new UserArrearsDTO();
        while (iter.hasNext()) {
            UserArrearsDTO userArrearsDTO=new UserArrearsDTO();
            int houseId = iter.next();
            userArrearsDTO.setName(houseIdMap.get(houseId));
            final BigDecimal[] totalAmount = {new BigDecimal(0)};
//            for (int i=0;i<)
            multimap.get(houseId).forEach(m->{
                userArrearsDTO.getArrears().add(new ArrearsDTO(m.getYearName(),m.getActualPayable()));
                totalAmount[0] = totalAmount[0].add(m.getActualPayable());
                userArrearsDTO.setTotalAmount(totalAmount[0]);
                userArrearsDTO.setDate(DateUtil.format(new Date(),"yyyy年MM月dd日"));
            });
            userArrearsDTOS.add(userArrearsDTO);
        }
        mapData.put("users",userArrearsDTOS);
        return mapData;
    }

    @Override
    public List<ArrearsDTO> getZonesArreas(SearchParam searchParam) {
        return baseMapper.selectZonesArreas(searchParam);
    }

    @Override
    public List<ArrearsDTO> getBuildingsArreas(SearchParam searchParam) {
        return baseMapper.selectBuildingsArreas(searchParam);
    }
    @Override
    public List<ArrearsDTO> getUnitssArreas(SearchParam searchParam) {
        return baseMapper.selectUnitssArreas(searchParam);
    }
}
